package com.example.message_queue.mqserver.datacenter;

import com.example.message_queue.MessageQueueApplication;
import com.example.message_queue.mqserver.core.*;
import com.example.message_queue.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-25
 * Time:15:40
 * Text：通过这个类，整合数据库操作
 */
public class DataBaseManger {
    private MetaMapper metaMapper;

    //数据库初始化
    public void init(){
        //手动获取MeataMapper
        metaMapper = MessageQueueApplication.context.getBean(MetaMapper.class);

        if(!checkDBExists()){
            File dataDir=new File("./data");
            dataDir.mkdirs();
            //不存在创建数据库和默认数据
            createTable();
            createDefaultData();
            System.out.println();
        }else{
            System.out.println("数据库已经存在");
        }

    }
    public void delete(){
        File file=new File("./data/meta.db");
        boolean ret= file.delete();
        if(ret){
            System.out.println("[DataBaseManger] delete:删除数据库成功");
        }else{
            System.out.println("[DataBaseManger] delete:删除数据库失败");
        }
        File file2=new File("./data");
        file2.delete();
        if(ret){
            System.out.println("[DataBaseManger] delete:删除数据库目录成功");
        }else{
            System.out.println("[DataBaseManger] delete:删除数据库目录失败");
        }
    }

    private boolean checkDBExists() {
        File file=new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }
    private void createTable() {
        metaMapper.createBindingTable();;
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        System.out.println("建表成功");

    }
    private void createDefaultData() {
        //构造一个匿名交换机
        Exchange exchange = new Exchange();
        exchange.setName("默认交换机");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        System.out.println("数据装载完毕");
        metaMapper.insertExchange(exchange);
    }



    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }
    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchange();
    }
    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }
    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }
    public List<MSGQueue> selectAllqueue(){
        return metaMapper.selectAllMSGQueue();
    }
    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBinding(){
        return metaMapper.selectAllBinding();
    }
    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }
}
